package com.rambler.fileupload.controller;

import com.rambler.api.fileupload.FileUploadProxy;
import com.rambler.api.fileupload.dto.ImageProcessOption;
import com.rambler.common.response.CommonResponse;
import com.rambler.api.fileupload.dto.FileUploadDTO;
import com.rambler.fileupload.facade.FileUploadFacadeService;
import com.rambler.api.fileupload.vo.FileUploadVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;

/**
 * @author suhao
 */
@RestController
@RequestMapping("/fs")
public class FileUploadController implements FileUploadProxy {

    @Resource
    private FileUploadFacadeService fileService;

//    @PostMapping("/upload")
//    @Operation(summary = "上传文件（自动去重）", description = "不对文件进行压缩")
//    @Override
//    public CommonResponse<FileUploadVO> upload(
//            @Parameter(description = "待上传的文件", required = true)
//            @RequestPart MultipartFile file,
//            @Parameter(description = "上传文件路径, OSS上传时路径第一位为bucketName", required = true)
//            @RequestParam String path,
//            @Parameter(description = "文件处理选型, 如图片压缩, excel解析", required = true)
//            FileUploadDTO dto) {
//        // 边界判断
//        if (null == file || null == path || path.isEmpty()) {
//            return CommonResponse.failure("文件不存在或者path不存在");
//        }
//        if (null == dto.getUploadType() || dto.getUploadType().isEmpty()) {
//            return CommonResponse.failure("未指定上传类型是OSS还是LOCAL");
//        }
//        return fileService.uploadFile(file, path, dto);
//    }

    @PostMapping("/upload")
    @Operation(summary = "上传文件（自动去重）", description = "不对文件进行压缩")
    @Override
    public CommonResponse<FileUploadVO> upload(
            @Parameter(description = "待上传的文件", required = true)
            @RequestPart MultipartFile file,
            @Parameter(description = "上传文件路径, OSS上传时路径第一位为bucketName", required = true)
            @RequestParam(value = "path") String path,
            @Parameter(description = "上传类型", required = true)
            @RequestParam(value = "uploadType") String uploadType,
            @Parameter(description = "是否公共读")
            @RequestParam(value = "isPublic", required = false) String isPublic,
            @Parameter(description = "输出格式, 如:jpg, webp(图片处理选项)")
            @RequestParam(value = "outputFormat", required = false) String outputFormat,
            @Parameter(description = "压缩系数, 范围:0-1(图片处理选项)")
            @RequestParam(value = "quality", required = false) String quality,
            @Parameter(description = "裁剪选项, 如: 0,0,100,100(图片处理选项)")
            @RequestParam(value = "crop", required = false) String crop,
            @Parameter(description = "缩放选项, 如: 100,100(图片处理选项)")
            @RequestParam(value = "resize", required = false) String resize
    ) {
        // 边界判断
        if (null == file || null == path || path.isEmpty()) {
            return CommonResponse.failure("文件不存在或者path不存在");
        }
        if (null == uploadType || uploadType.isEmpty()) {
            return CommonResponse.failure("未指定上传类型是OSS还是LOCAL");
        }
        FileUploadDTO dto = new FileUploadDTO();
        dto.setUploadType(uploadType);
        if (isPublic != null && !isPublic.isEmpty()) {
            dto.setIsPublic(Integer.parseInt(isPublic));
        }
        // 图片处理参数
        ImageProcessOption imageProcessOption = new ImageProcessOption();
        // 压缩系数
        if (quality != null && !quality.isEmpty()) {
            imageProcessOption.setQuality(Float.parseFloat(quality));
        }
        // 输出格式
        imageProcessOption.setOutputFormat(outputFormat);
        // 裁剪
        if (crop != null && !crop.isEmpty()) {
            String[] split = crop.split(",");
            if (split.length == 4) {
                imageProcessOption.setCrop(new ImageProcessOption.CropRegion(
                        Integer.parseInt(split[0]),
                        Integer.parseInt(split[1]),
                        Integer.parseInt(split[2]),
                        Integer.parseInt(split[3])));
            }
        }
        // 缩放
        if (resize != null && !resize.isEmpty()) {
            String[] split = resize.split(",");
            if (split.length == 2) {
                ImageProcessOption.ResizeOption resizeOption = new ImageProcessOption.ResizeOption(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
                imageProcessOption.setResize(resizeOption);
            }
        }
        dto.setImageProcessOption(imageProcessOption);
        return fileService.uploadFile(file, path, dto);
    }


    @PostMapping("/getById")
    @Operation(summary = "根据id查询文件信息", description = "根据id查询文件信息")
    @Override
    public CommonResponse<FileUploadVO> selectById(String id) {
        if (null == id || id.isEmpty()) {
            return CommonResponse.failure("id不存在");
        }
        return CommonResponse.success(fileService.selectById(id));
    }

}
